package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class UserLogIN extends LibrarySession{
    private static String loggedInUserId = null;
    public static boolean logIn(String userId, String password) {

            int i;
            for (i = 0; users[i] != null; i++) {
                if (users[i].getId().equals(userId)) {
                    if (users[i].getPassword().equals(password)) {
                        users[i].setLoggedIn(true);
                        loggedInUserId = userId;
                        if (i == 0)
                            LibrarySession.setLibrarianLoggedIn(true);
                        else LibrarySession.setUserLoggedIn(true);
                        System.out.println("Log in Successful!");
                        return true;
                    }
                    else {
                        System.out.print("Incorrect Password...Please try again!");
                    }
                    break;
                }
            }
            if (i == 5) {
                System.out.println("Incorrect User Id...Please try again!");
            }
        return false;
    }

    public static boolean logOut() {
        for (int i = 0; users[i] != null ; i++) {
            if (users[i].getId().equals(loggedInUserId)) {
                users[i].setLoggedIn(true);
                loggedInUserId = null;
                System.out.println("Log out successful");
                return true;
            }
        }
        return false;
    }
}
